home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 8 / Mac Magazin and MacEasy Magazine CD - Issue 08.iso / Sharewarebibliothek / Utilities / Minimalist Clock 1.0 / src / main.c < prev    next >
C/C++ Source or Header  |  1994-12-07  |  6KB  |  289 lines

  1. /* ----------------------------------------------------------------------
  2.  
  3.     Minimalist Clock
  4.     version 1.0.0
  5.     7 December 1994
  6.     
  7.     Written by: Paul Celestin
  8.     
  9.     This simple application does nothing but display a simple clock in
  10.     a rectangle on your display. You can move the clock around, and you
  11.     can also check the date by clicking once on the clock.
  12.     
  13.     941207 - 1.0.0 initial release
  14.  
  15. ---------------------------------------------------------------------- */
  16.  
  17.  
  18. /* ----------------------------------------------------------------------
  19. includes
  20. ---------------------------------------------------------------------- */
  21.  
  22. #include    "the_defines.h"
  23. #include    "the_globals.h"
  24. #include    "the_prototypes.h"
  25.  
  26. Boolean            gDone,
  27.                 gWNEImplemented,
  28.                 gInBackground,
  29.                 gDrawDate = FALSE;
  30. long            gTicksOld = 0, gTicksNew = 0;
  31. EventRecord        gTheEvent;
  32. MenuHandle        gAppleMenu,
  33.                 gFileMenu,
  34.                 gEditMenu;
  35. WindowPtr        gClock;
  36.  
  37. /* ----------------------------------------------------------------------
  38. main - here is where it all began...
  39. ---------------------------------------------------------------------- */
  40. void main()
  41. {
  42.     InitToolBox();
  43.     InitMenuBar();
  44.     MainLoop();
  45.     ExitToShell();
  46. }
  47.  
  48. /* ----------------------------------------------------------------------
  49. InitToolBox
  50. ---------------------------------------------------------------------- */
  51. void InitToolBox()
  52. {
  53.     InitGraf(&qd.thePort);
  54.     InitFonts();
  55.     FlushEvents(everyEvent, 0);
  56.     InitWindows();
  57.     InitMenus();
  58.     TEInit();
  59.     InitDialogs(0L);
  60.     InitCursor();
  61. }
  62.  
  63. /* ----------------------------------------------------------------------
  64. InitMenuBar
  65. ---------------------------------------------------------------------- */
  66. void InitMenuBar()
  67. {
  68.     Handle    myMenuBar;
  69.     
  70.     myMenuBar = GetNewMBar(MENU_BASE_ID);
  71.     if (myMenuBar == NIL)
  72.         SysBeep(1);
  73.     else
  74.         SetMenuBar(myMenuBar);
  75.     
  76.     gAppleMenu = GetMHandle(MENU_APPLE_ID);
  77.     if (gAppleMenu == NIL)
  78.         SysBeep(1);
  79.     else
  80.         AddResMenu(gAppleMenu,'DRVR');
  81.     
  82.     gFileMenu = GetMHandle(MENU_FILE_ID);
  83.     if (gFileMenu == NIL)
  84.         SysBeep(1);
  85.  
  86.     gEditMenu = GetMHandle(MENU_EDIT_ID);
  87.     if (gEditMenu == NIL)
  88.         SysBeep(1);
  89.     
  90.     DrawMenuBar();
  91. }
  92.  
  93. /* ----------------------------------------------------------------------
  94. MainLoop
  95. ---------------------------------------------------------------------- */
  96. void MainLoop()
  97. {
  98.     RgnHandle    cursorRgn;
  99.     Boolean        gotEvent;
  100.     
  101.     gDone = false;
  102.     gInBackground = false;
  103.     
  104.     cursorRgn = NewRgn();
  105.     
  106.     gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM,ToolTrap) !=
  107.         NGetTrapAddress(UNIMPL_TRAP_NUM,ToolTrap));
  108.     
  109.     CreateClock();
  110.     
  111.     while (gDone == false)
  112.     {
  113.         if (gWNEImplemented)
  114.         gotEvent =
  115.             WaitNextEvent(everyEvent,&gTheEvent,MIN_SLEEP,cursorRgn);
  116.         else
  117.         {
  118.             SystemTask();
  119.             gotEvent = GetNextEvent(everyEvent,&gTheEvent);
  120.         }
  121.         
  122.         if (gotEvent)
  123.             Do();
  124.         else
  125.             DoIdle();
  126.     }
  127. }
  128.  
  129. /* ----------------------------------------------------------------------
  130. Do
  131. ---------------------------------------------------------------------- */
  132. void Do()
  133. {
  134.     char    c;
  135.     
  136.     switch (gTheEvent.what)
  137.     {
  138.         case nullEvent:
  139.             DoIdle();
  140.             break;
  141.         case mouseDown:
  142.             DoMouseDown();
  143.             break;
  144.         case keyDown:
  145.         case autoKey:
  146.             c = gTheEvent.message & charCodeMask;
  147.             if ((gTheEvent.modifiers & cmdKey) != 0)
  148.             {
  149.                 DoMenu(MenuKey(c));
  150.             }
  151.             break;
  152.         case activateEvt:
  153.             break;
  154.         case updateEvt:
  155.             break;
  156.         case app4Evt:
  157.             if ((gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING)
  158.             {
  159.                 gInBackground = (gTheEvent.message & 0x01) == 0;
  160.             }
  161.             else
  162.                 DoIdle();
  163.             break;
  164.     }
  165. }
  166.  
  167. /* ----------------------------------------------------------------------
  168. DoIdle
  169. ---------------------------------------------------------------------- */
  170. void DoIdle()
  171. {
  172.     gTicksNew = TickCount();
  173.     if ((gTicksNew - gTicksOld) > UPDATE_INTERVAL)
  174.     {
  175.         gTicksOld = gTicksNew;
  176.         UpdateClock();
  177.     }
  178. }
  179.  
  180. /* ----------------------------------------------------------------------
  181. DoUpdate
  182. ---------------------------------------------------------------------- */
  183. void DoUpdate(window)
  184. WindowPtr window;
  185. {
  186.     GrafPtr    savedPort;
  187.     
  188.     GetPort(&savedPort);
  189.     SetPort(window);
  190.     BeginUpdate(window);
  191.     UpdateClock();
  192.     EndUpdate(window);
  193.     SetPort(savedPort);
  194. }
  195.  
  196. /* ----------------------------------------------------------------------
  197. DoMouseDown
  198. ---------------------------------------------------------------------- */
  199. void DoMouseDown()
  200. {
  201.     WindowPtr    window;
  202.     short int    thePart;
  203.     long int    menuChoice, windSize, newSize;
  204.     Boolean        doZoom, doGoAway;
  205.     
  206.     thePart = FindWindow(gTheEvent.where,&window);
  207.     switch (thePart)
  208.     {
  209.         case inMenuBar:
  210.             menuChoice = MenuSelect(gTheEvent.where);
  211.             DoMenu(menuChoice);
  212.             break;
  213.         case inSysWindow:
  214.             SystemClick(&gTheEvent,window);
  215.             break;
  216.         case inContent:
  217.             if (window != FrontWindow())
  218.                 SelectWindow(window);
  219.             break;
  220.         case inGrow:
  221.             newSize = GrowWindow(window,gTheEvent.where,&(qd.screenBits.bounds));
  222.             SizeWindow(window,LoWord(newSize),HiWord(newSize),false);
  223.             DoUpdate(window);
  224.             break;
  225.         case inDrag:
  226.             gDrawDate = TRUE;
  227.             DragWindow(window,gTheEvent.where,&(qd.screenBits.bounds));
  228.             DoUpdate(window);
  229.             break;
  230.         case inZoomIn:
  231.             if (doZoom = TrackBox(window,gTheEvent.where,inZoomIn))
  232.             {
  233.                 SizeWindow(window,200,200,false);
  234.                 DoUpdate(window);
  235.             }
  236.             break;
  237.         case inZoomOut:
  238.             if (doZoom = TrackBox(window,gTheEvent.where,inZoomOut))
  239.             {
  240.                 short winHeight,winWidth;
  241.  
  242.                 winHeight = qd.screenBits.bounds.bottom - 43;
  243.                 winWidth = qd.screenBits.bounds.right - 5;
  244.                 SizeWindow(window,winWidth,winHeight,false);
  245.                 MoveWindow(window,2,LMGetMBarHeight() + 20,false);
  246.                 DoUpdate(window);
  247.             }
  248.             break;
  249.             break;
  250.         case inGoAway:
  251.             if (doGoAway = TrackGoAway(window,gTheEvent.where))
  252.                 CloseWindow(window);
  253.             break;
  254.         default:
  255.             break;
  256.     }
  257. }
  258.  
  259. /* ----------------------------------------------------------------------
  260. DoMenu
  261. ---------------------------------------------------------------------- */
  262. void DoMenu(menuChoice)
  263. long int menuChoice;
  264. {
  265.     int    theMenu;
  266.     int    theItem;
  267.     
  268.     if (menuChoice != 0)
  269.     {
  270.         theMenu = HiWord(menuChoice);
  271.         theItem = LoWord(menuChoice);
  272.         switch (theMenu)
  273.         {
  274.             case MENU_APPLE_ID:
  275.                 DoMenuApple(theItem);
  276.                 break;
  277.             case MENU_FILE_ID:
  278.                 DoMenuFile(theItem);
  279.                 break;
  280.             case MENU_EDIT_ID:
  281.                 DoMenuEdit(theItem);
  282.                 break;
  283.             default:
  284.                 break;
  285.         }
  286.         HiliteMenu(0);
  287.     }
  288. }
  289.